package edu.umassd.hciproject;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Context;

public class MyListsManager  {
	public final static String FILENAME = "my_lists.json";

	MyStorageManager storage;
	
	public MyListsManager(Context context) {
		storage = new MyStorageManager(context, FILENAME);
	}
	
	public ArrayList<String> getNames() {
		return storage.getNames();
	}
	
	// Add new list or update existing one
	public void putList(String name, ArrayList<String> products) {
		JSONArray jsProducts = new JSONArray();
		for (String product: products)
			jsProducts.put(product);
		storage.putElement(name, jsProducts);
	}
	
	public void removeList(String name) {
		storage.removeElement(name);
	}
	
	public void update() {
		storage.reload();
	}
	
	public ArrayList<String> getList(String name) {
		JSONArray jsProducts = (JSONArray) storage.getContentsOf(name);
		ArrayList<String> products = new ArrayList<String>();
		for (int i=0; i<jsProducts.length(); i++) {
			try {
				products.add(jsProducts.getString(i));
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return products;
	}

}
